﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ComponentModel;
using Microsoft.Practices.Unity;

namespace VpPrism.Modules.ModuleOne.ViewModels
{
  /// <summary>
  /// ViewModel du ModuleOne
  /// </summary>
  public class ModuleOneMainVM: IModuleOneMainVM, INotifyPropertyChanged
  {
    #region Fields

    private readonly IUnityContainer container;

    private String _Message;

    /// <summary>
    /// Message envoyé par le ModuleTwo
    /// </summary>
    public String Message
    {
      get { return _Message; }
      set 
      { 
        _Message = value;
        OnPropertyChanged("Message");
      }
    }
    #endregion
    
    #region IModuleOneMainVM Members

    /// <summary>
    /// Vue principale
    /// </summary>
    public VpPrism.Modules.ModuleOne.Views.IModuleOneMainV MainView {get; private set;}

    #endregion

    /// <summary>
    /// Constructeur. Les paramètres sont pré-remplis par le UnityContainer.
    /// </summary>
    /// <param name="container"></param>
    /// <param name="mainView"></param>
    public ModuleOneMainVM(IUnityContainer container, VpPrism.Modules.ModuleOne.Views.IModuleOneMainV mainView)
    {
      this.container = container;
      this.MainView = mainView;
      this.MainView.SetModel(this);
      this.container.Resolve<Microsoft.Practices.Composite.Events.IEventAggregator>().GetEvent<VpPrism.Events.User.UserActionEvent>().Subscribe(OnUserAction, Microsoft.Practices.Composite.Presentation.Events.ThreadOption.UIThread, true);
    }

    private void OnUserAction(VpPrism.Events.User.UserActionEventArgs e)
    {
      if (e.Action == VpPrism.Common.Enums.UserAction.SendMessage)
      {
        if (e.Data is String)
        {
          this.Message = e.Data as String;
        }
      }
    }

    #region INotifyPropertyChanged Members
    /// <summary>
    /// Implémentation de INotifyPropertyChanged.
    /// </summary>
    public event PropertyChangedEventHandler PropertyChanged;

    /// <summary>
    /// Fonction de Raise de l'évènement PropertyChanged.
    /// </summary>
    /// <param name="PropertyName"></param>
    protected void OnPropertyChanged(string PropertyName)
    {
      if (PropertyChanged != null)
      {
        PropertyChanged(this, new PropertyChangedEventArgs(PropertyName));
      }
    }

    #endregion
  }
}
